package xyz.hubery.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.hubery.domain.Sms;
import xyz.hubery.model.R;

import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "短信服务控制器")
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @PostMapping("/sendTo")
    @ApiOperation("发送短信")
    public R<Boolean> sendSms(@RequestBody @Validated Sms sms) {
        // 先查缓存
        if(Boolean.TRUE.equals(redisTemplate.hasKey("SMS:" + sms.getMobile()))){
            return R.fail("请勿频繁发送");
        }
        redisTemplate.opsForValue().set("SMS:" + sms.getMobile() ,"123456",5, TimeUnit.MINUTES);
        return R.ok();
    }




}
